package com.clp.inflxudb.core.flux_function;

import com.clp.inflxudb.core.Flux;

import java.util.ArrayList;
import java.util.List;

/**
 * 按键名列表排序
 */
public class SortFluxFunction implements FluxFunction {
    private List<String> keyNames = new ArrayList<>();
    private boolean desc = true;

    public SortFluxFunction(String keyName, boolean desc) {
        if (keyName != null) {
            keyNames.add(keyName);
        }
        this.desc = desc;
    }

    public SortFluxFunction(List<String> keyNames, boolean desc) {
        if (keyNames != null && !keyNames.isEmpty()) {
            this.keyNames = keyNames;
        }
        this.desc = desc;
    }

    @Override
    public void doFunction(Flux flux) {
        // 如果没有排序条件就不排序
        if (keyNames.isEmpty()) {
            return;
        }
        // 按时间分组
        flux.sort(keyNames, desc);
    }
}
